home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 …ember: Reference Library / Apple Developer Reference Library (December 1999) (Disk 1).iso / pc / technical documentation / develop / develop issue 28 / develop issue 28 code / qd3d listings.c
Encoding:
Text File  |  1996-10-04  |  5.9 KB  |  191 lines

  1. //-------------------------------------------------------------------
  2. // Listing 1. Creating a four-faced polyhedron
  3. //-------------------------------------------------------------------
  4.  
  5. TQ3ColorRGB                polyhedronColor;
  6. TQ3PolyhedronData        polyhedronData;
  7. TQ3GeometryObject        polyhedron;
  8. TQ3Vector3D                normal;
  9.  
  10. static TQ3Vertex3D    vertices[7] = {
  11.     { { -1.0,  1.0,  0.0 }, NULL },
  12.     { { -1.0, -1.0,  0.0 }, NULL },
  13.     { {  0.0,  1.0,  1.0 }, NULL },
  14.     { {  0.0, -1.0,  1.0 }, NULL },
  15.     { {  2.0,  1.0,  1.0 }, NULL },
  16.     { {  2.0, -1.0,  0.0 }, NULL },
  17.     { {  0.0, -1.0,  1.0 }, NULL }
  18. };
  19. TQ3PolyhedronTriangleData    triangles[4] = {
  20.     {  /* Face 0 */
  21.         { 0, 1, 2 },                                            /* vertexIndices */
  22.         kQ3PolyhedronEdge01 | kQ3PolyhedronEdge20,    /* edgeFlag */
  23.         NULL                                                /* triangleAttributeSet */
  24.     },
  25.     {  /* Face 1 */
  26.         { 1, 3, 2 },
  27.         kQ3PolyhedronEdge01 | kQ3PolyhedronEdge12,
  28.         NULL
  29.     },
  30.     {  /* Face 2 */
  31.         { 2, 3, 4 },
  32.         kQ3PolyhedronEdgeAll,
  33.         NULL
  34.     },
  35.     {  /* Face 3 */
  36.         { 6, 5, 4 },
  37.         kQ3PolyhedronEdgeAll,
  38.         NULL
  39.     }
  40. };
  41.  
  42. /* Set up vertices, edges, and triangular faces. */
  43. polyhedronData.numVertices        = 7;
  44. polyhedronData.vertices            = vertices;    
  45. polyhedronData.numEdges            = 0;
  46. polyhedronData.edges                = NULL;
  47. polyhedronData.numTriangles    = 4;
  48. polyhedronData.triangles        = triangles;
  49.  
  50. /* Inherit the attribute set from the current state. */
  51. polyhedronData.polyhedronAttributeSet = NULL;
  52.     
  53. /* Put a normal on the first vertex. */
  54. Q3Vector3D_Set(&normal, -1, 0, 1);
  55. Q3Vector3D_Normalize(&normal, &normal);
  56. vertices[0].attributeSet = Q3AttributeSet_New();
  57. Q3AttributeSet_Add(vertices[0].attributeSet, kQ3AttributeTypeNormal, 
  58.     &normal);
  59.  
  60. /* Same normal on the second. */
  61. vertices[1].attributeSet = 
  62.     Q3Shared_GetReference(vertices[0].attributeSet);
  63.     
  64. /* Different normal on the third. */
  65. Q3Vector3D_Set(&normal, -0.5, 0.0, 1.0);
  66. Q3Vector3D_Normalize(&normal, &normal);
  67. vertices[2].attributeSet = Q3AttributeSet_New();
  68. Q3AttributeSet_Add(vertices[2].attributeSet, kQ3AttributeTypeNormal, 
  69.     &normal);
  70. /* Same normal on the fourth. */
  71. vertices[3].attributeSet = 
  72.     Q3Shared_GetReference(vertices[2].attributeSet);
  73.  
  74. /* Put a color on the third triangle. */
  75. triangles[3].triangleAttributeSet = Q3AttributeSet_New();
  76. Q3ColorRGB_Set(&polyhedronColor, 0, 0, 1);
  77. Q3AttributeSet_Add(triangles[3].triangleAttributeSet,
  78.     kQ3AttributeTypeDiffuseColor, &polyhedronColor);
  79.  
  80. /* Create the polyhedron object. */
  81. polyhedron = Q3Polyhedron_New(&polyhedronData);
  82.  
  83. // ...    /* Dispose of attributes created and referenced. */
  84.  
  85.  
  86. //-------------------------------------------------------------------
  87. // Listing 2. Using an edge list to specify the edges of a polyhedron
  88. //-------------------------------------------------------------------
  89.  
  90. polyhedronData.numEdges = 8;
  91. polyhedronData.edges = malloc(8 * sizeof(TQ3PolyhedronEdgeData));
  92.     
  93. polyhedronData.edges[0].vertexIndices[0]        = 0;
  94. polyhedronData.edges[0].vertexIndices[1]        = 1;
  95. polyhedronData.edges[0].triangleIndices[0]    = 0;
  96. polyhedronData.edges[0].triangleIndices[1]    = kQ3ArrayIndexNULL;
  97. polyhedronData.edges[0].edgeAttributeSet        = NULL;
  98.     
  99. polyhedronData.edges[1].vertexIndices[0]        = 2;
  100. polyhedronData.edges[1].vertexIndices[1]        = 0;
  101. polyhedronData.edges[1].triangleIndices[0]    = 0;
  102. polyhedronData.edges[1].triangleIndices[1]    = kQ3ArrayIndexNULL;
  103. polyhedronData.edges[1].edgeAttributeSet        = NULL;
  104.  
  105. polyhedronData.edges[2].vertexIndices[0]        = 1;
  106. polyhedronData.edges[2].vertexIndices[1]        = 3;
  107. polyhedronData.edges[2].triangleIndices[0]    = 1;
  108. polyhedronData.edges[2].triangleIndices[1]     = kQ3ArrayIndexNULL;
  109. polyhedronData.edges[2].edgeAttributeSet        = NULL;
  110.  
  111. polyhedronData.edges[3].vertexIndices[0]        = 3;
  112. polyhedronData.edges[3].vertexIndices[1]        = 2;
  113. polyhedronData.edges[3].triangleIndices[0]    = 1;
  114. polyhedronData.edges[3].triangleIndices[1]    = 2;
  115. polyhedronData.edges[3].edgeAttributeSet        = NULL;
  116.  
  117. //...    /* Specify the rest of the edges. */
  118.  
  119.  
  120. //-------------------------------------------------------------------
  121. // Listing 3. Creating a mesh
  122. //-------------------------------------------------------------------
  123.  
  124. static TQ3Vertex3D    vertices[7] = {
  125.     { { -1.0,  1.0,  0.0 }, NULL },
  126.     { { -1.0, -1.0,  0.0 }, NULL },
  127.     { {  0.0,  1.0,  1.0 }, NULL },
  128.     { {  0.0, -1.0,  1.0 }, NULL },
  129.     { {  2.0,  1.0,  1.0 }, NULL },
  130.     { {  2.0, -1.0,  0.0 }, NULL },
  131.     { {  0.0, -1.0,  1.0 }, NULL },
  132. };
  133. TQ3MeshVertex            meshVertices[7], tmp[4];
  134. TQ3GeometryObject        mesh;
  135. TQ3MeshFace                face01, face2, face3;
  136. TQ3AttributeSet        faceAttributes;
  137. unsigned long            i;
  138. TQ3ColorRGB                color;
  139. TQ3Vector3D                normal;
  140.  
  141. /* Add normals to some of the vertices. */
  142. vertices[0].attributeSet = Q3AttributeSet_New();
  143. Q3Vector3D_Set(&normal, -1, 0, 1);
  144. Q3Vector3D_Normalize(&normal, &normal);
  145. Q3AttributeSet_Add(vertices[0].attributeSet, kQ3AttributeTypeNormal, 
  146.     &normal);
  147.  
  148. vertices[1].attributeSet =
  149.     Q3Shared_GetReference(vertices[0].attributeSet);
  150.  
  151. vertices[2].attributeSet = Q3AttributeSet_New();
  152. Q3Vector3D_Set(&normal, -0.5, 0.0, 1.0);
  153. Q3Vector3D_Normalize(&normal, &normal);
  154. Q3AttributeSet_Add(vertices[2].attributeSet, kQ3AttributeTypeNormal, 
  155.     &normal);
  156.  
  157. vertices[3].attributeSet = 
  158.     Q3Shared_GetReference(vertices[2].attributeSet);
  159.  
  160. /* Create the mesh. */
  161. mesh = Q3Mesh_New();
  162.  
  163. /* Create the mesh vertices. */
  164. for (i = 0; i < 7; i++) {
  165.     meshVertices[i] = Q3Mesh_VertexNew(mesh, &vertices[i]);
  166. }
  167.  
  168. /* Create a quad equal to the first two triangles in the polyhedron. */
  169. tmp[0] = meshVertices[0];
  170. tmp[1] = meshVertices[1];
  171. tmp[2] = meshVertices[3];
  172. tmp[3] = meshVertices[2];
  173. face01 = Q3Mesh_FaceNew(mesh, 4, tmp, NULL);
  174.  
  175. /* Create other faces. */
  176. tmp[0] = meshVertices[2];
  177. tmp[1] = meshVertices[3];
  178. tmp[2] = meshVertices[4];
  179. face2 = Q3Mesh_FaceNew(mesh, 3, tmp, NULL);
  180.  
  181. tmp[0] = meshVertices[6];
  182. tmp[1] = meshVertices[5];
  183. tmp[2] = meshVertices[4];
  184. face3 = Q3Mesh_FaceNew(mesh, 3, tmp, NULL);
  185.  
  186. /* Add an attribute set to the last face. */
  187. faceAttributes = Q3AttributeSet_New();
  188. Q3ColorRGB_Set(&color, 0, 0, 1);
  189. Q3AttributeSet_Add(faceAttributes, kQ3AttributeTypeDiffuseColor, &color);
  190. Q3Mesh_SetFaceAttributeSet(mesh, face3, faceAttributes);
  191.